import axios from 'axios'
import { getProductSaleDetail,getInventoryBalanceSheet } from '@/api/report'
import Pagination from '@/components/Pagination'

export default {
  name: 'InventoryBalanceSheet',
  components: { Pagination },
  data() {
    return {
      categoryOptions: [{
        value: 0,
        label: '全部'
      }],
      subCategoryOptions: [{
        value: 0,
        label: '全部'
      }],
      cateList: [],
      fileList: [],
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        category: 0,
        sub_category: 0,
        category_id: '',
        keyword: '',
        from_date: null,
        to_date: null
      },
      downloadLoading: false,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      datetime: null,
      statistics: null,
      down_url: '',
    }
  },
  async created() {
    this.getList()
    const cateList = await this.$store.dispatch('product/getCateList')
    this.cateList = cateList
    this.categoryOptions = this.categoryOptions.concat(cateList)

    console.log(process.env.VUE_APP_BASE_API)

  },
  methods: {
    getList() {
      this.listLoading = true
      getInventoryBalanceSheet().then(response => {
        this.list = response.list
        this.statistics = response.statistics
        this.total = response.total
        this.listLoading = false
      }).catch(error => {
        this.list = []
        this.total = 0
        this.listLoading = false
        Promise.reject(error)
      })
    },
    handleSelectChange() {
      const id = this.listQuery.category
      const list = [{
        value: 0,
        label: '全部'
      }]
      if (id === 0) {
        this.subCategoryOptions = list
      } else {
        const index = this.categoryOptions.findIndex(v => v.value === id)
        const item = this.categoryOptions[index]
        this.subCategoryOptions = list.concat(item.children)
      }
      this.listQuery.sub_category = 0
      this.handleSearch()
    },
    handleSearch() {
      if (this.listQuery.keyword == '') {
        this.listQuery.keyword = null
      }
      if (!this.datetime) {
        this.listQuery.from_date = null
        this.listQuery.to_date = null
      } else {
        this.listQuery.from_date = this.datetime[0]
        this.listQuery.to_date = this.datetime[1]
      }

      this.list = []
      this.listQuery.page = 1
      this.getList()
    },

    getSummaries(){
      if(!this.statistics){
        return
      }
      const statistics = this.statistics
      const sums = []
      sums[0] = '合计'
      sums[5] = statistics.total_amount

      return sums
    },

    exportData(){
      let url = process.env.VUE_APP_BASE_API + '/api/report/inventory-balance-sheet?action=export'
      let user = JSON.parse(window.localStorage.getItem('user'))
      axios({
        method:'get',
        url:url,
        responseType:'blob',
        headers:{
          'x-api-key':user.api_key
        }
      }).then((data) => {
        let blob = new Blob([data.data],{type: "application/vnd.ms-excel"});
        const fileName = '库存余额表.xlsx';
        const linkNode = document.createElement('a');
        linkNode.download = fileName; //a标签的download属性规定下载文件的名称
        linkNode.style.display = 'none';
        linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
        document.body.appendChild(linkNode);
        linkNode.click();  //模拟在按钮上的一次鼠标单击
        URL.revokeObjectURL(linkNode.href); // 释放URL 对象
        document.body.removeChild(linkNode);
      })
    },

  }
}
